<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="时间范围" prop="datePicker">
              <el-date-picker v-model="datePicker" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" @change="changeTimer" />
            </el-form-item>
            <el-form-item label="素材id" prop="materialId">
              <el-input v-model="queryParams.materialId" placeholder="请输入素材id" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="视频素材名称" prop="adPlatformMaterialName">
              <el-input v-model="queryParams.adPlatformMaterialName" placeholder="请输入视频素材名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="编辑姓名" prop="compileName">
              <el-input v-model="queryParams.compileName" placeholder="请输入编辑姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              <!--              <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['oce:materialEverydayCensus:export']"-->
              <!--                >导出</el-button-->
              <!--              >-->
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="never">
      <listView :list="sum" />
      <el-table v-loading="loading" :data="materialEverydayCensusList" @sort-change="sortChangeHandel" @selection-change="handleSelectionChange">
        <el-table-column label="素材">
          <template #default="{ row }">
            <div class="content-wrap">
              <div>
                素材ID:
                <el-text type="danger">
                  <span class="cursor-pointer underline" v-copyText="row.materialId">{{ row.materialId }}</span>
                </el-text>
              </div>
              <div>
                <!-- <div>素材名称:</div> -->
                <el-text type="danger">
                  <span class="cursor-pointer underline" v-copyText="row.adPlatformMaterialName">{{ row.adPlatformMaterialName }}</span>
                </el-text>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="总消耗" prop="statCost" sortable="custom">
          <template #default="{ row }">
            <span>{{ formatPrice(row.statCost) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="展示数" prop="showCnt" sortable="custom"></el-table-column>
        <el-table-column label="平均千次展现费用" prop="cpmPlatform" sortable="custom">
          <template #default="{ row }">
            <span>{{ row.cpmPlatform }}</span>
          </template>
        </el-table-column>
        <el-table-column label="点击数" prop="clickCnt" sortable="custom"></el-table-column>
        <el-table-column label="点击率" prop="ctr" sortable="custom">
          <template #default="{ row }">
            <span>{{ row.ctr }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="转化数" prop="convertCnt" sortable="custom"></el-table-column>
        <el-table-column label="转化成本" prop="conversionCost" sortable="custom"></el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="MaterialEverydayCensus" lang="ts">
import { listMaterialEverydayCensus } from '@/api/oce/materialEverydayCensus';
import { MaterialEverydayCensusVO, MaterialEverydayCensusQuery, MaterialEverydayCensusForm } from '@/api/oce/materialEverydayCensus/types';
import { formatYmd, formatPrice } from '@/utils';
import dayjs from 'dayjs';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const materialEverydayCensusList = ref<MaterialEverydayCensusVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const initFormData: MaterialEverydayCensusForm = {
  id: undefined,
  timestamps: undefined,
  thatDayDate: undefined,
  thirdAdvertiserId: undefined,
  thirdAdvertiserName: undefined,
  thirdAdvertiserAppCode: undefined,
  cdpProjectId: undefined,
  cdpProjectName: undefined,
  cdpPromotionId: undefined,
  cdpPromotionName: undefined,
  materialId: undefined,
  adPlatformMaterialName: undefined,
  statCost: undefined,
  showCnt: undefined,
  cpmPlatform: undefined,
  clickCnt: undefined,
  ctr: undefined,
  cpcPlatform: undefined,
  convertCnt: undefined,
  conversionCost: undefined,
  conversionRate: undefined,
  deepConvertCnt: undefined,
  deepConvertCost: undefined,
  deepConvertRate: undefined,
  active: undefined,
  activeCost: undefined,
  activeRate: undefined,
  activePay: undefined,
  activePayCost: undefined,
  activePayRate: undefined,
  gamePayCount: undefined,
  gamePayCost: undefined,
  totalPlay: undefined,
  validPlay: undefined,
  validPlayCost: undefined,
  validPlayRate: undefined,
  playDuration3s: undefined,
  validPlayOfMille: undefined,
  validPlayCostOfMille: undefined,
  play25FeedBreak: undefined,
  play50FeedBreak: undefined,
  play75FeedBreak: undefined,
  play99FeedBreak: undefined,
  averagePlayTimePerPlay: undefined,
  playOverRate: undefined,
  wifiPlayRate: undefined,
  cardShow: undefined,
  dyLike: undefined,
  dyComment: undefined,
  dyShare: undefined,
  dislikeCnt: undefined,
  reportCnt: undefined,
  iesChallengeClick: undefined,
  iesMusicClick: undefined,
  compileName: undefined,
  startDate: undefined,
  endDate: undefined,
  locationClick: undefined
};
const data = reactive<PageData<MaterialEverydayCensusForm, MaterialEverydayCensusQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    materialId: undefined,
    adPlatformMaterialName: undefined,
    compileName: undefined,
    orderByColumn: undefined,
    isAsc: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '广告每日数据记录id不能为空', trigger: 'blur' }],
    timestamps: [{ required: true, message: '当日时间戳不能为空', trigger: 'blur' }],
    thatDayDate: [{ required: true, message: '当天日期不能为空', trigger: 'blur' }],
    thirdAdvertiserId: [{ required: true, message: '第三方广告主id不能为空', trigger: 'blur' }],
    cdpProjectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
    cdpPromotionId: [{ required: true, message: '广告id不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);
const datePicker = ref<any>([dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]);
const sum = ref([]);

/** 查询巨量素材每日数据统计列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMaterialEverydayCensus(queryParams.value);
  materialEverydayCensusList.value = res.rows;
  sum.value = res.sum || [];
  total.value = res.total;
  loading.value = false;
};

const sortChangeHandel = ({ order, prop }) => {
  queryParams.value.orderByColumn = prop;
  queryParams.value.isAsc = order === 'ascending' ? 'asc' : 'desc';
  getList();
};

const changeTimer = (e) => {
  queryParams.value.startDate = e ? formatYmd(e[0]) : '';
  queryParams.value.endDate = e ? formatYmd(e[1]) : '';
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  datePicker.value = [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')];
  queryParams.value.startDate = datePicker.value[0];
  queryParams.value.endDate = datePicker.value[1];
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: MaterialEverydayCensusVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'oce/materialEverydayCensus/export',
    {
      ...queryParams.value
    },
    `materialEverydayCensus_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  queryParams.value.startDate = datePicker.value[0];
  queryParams.value.endDate = datePicker.value[1];
  getList();
});
</script>
